package com.yuxi;

/**
 * 一道经常出现的面试题,怎么保证T1,T2,T3三个同时启动的线程按照顺序执行,例如顺序T1,T2,T3
 * Created by yuxi on 17/1/27.
 */
public class JoinDemo {
    public static void main(String[] args) {
        final Thread T1 = new Thread(new Runnable() {
            public void run() {
                try {
                    Thread.sleep(10);
                    System.out.println("this is  t1");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        final Thread T2 = new Thread(new Runnable() {
            public void run() {
                try {
                    T1.join();
                    Thread.sleep(10);
                    System.out.println("this is  t2");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        final Thread T3 = new Thread(new Runnable() {
            public void run() {
                try {
                    T2.join();
                    Thread.sleep(10);
                    System.out.println("this is  t3");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        T2.start();
        T3.start();
        T1.start();
    }

}
